<?php
/**
 * 添加未签收记录
 * User: Administrator
 * Date: 2018/8/7/007
 * Time: 20:57
 */
namespace app\lib\Order\OrderSign;
use app\lib\Order\IsOrder\IsOrder;
use app\lib\Order\OrderExtensionConfig\GetOrderBindingExtension;
use app\lib\Order\OrderExtensionConfig\OrderBindingExtension;
use app\model\OrderSignRecordModel;

class AddOrderSignRecord
{
    protected $orderId;
    protected $opUid;
    public function __construct($orderId,$opUid)
    {
        $this->orderId = $orderId;
        $this->opUid = $opUid;
    }

    /**
     * 验证
     * @return bool
     * @throws \Exception
     */
    protected function check(){
        if(!(new IsOrder($this->orderId))->isExecuteOrder()){
            exception('工单状态异常');
        }
        if((new GetOrderBindingExtension($this->orderId))->isOrderBindingSign()){
            exception('工单存在未签收项');
        }
        return true;
    }

    /**
     * 执行代码
     * @return bool
     * @throws \Exception
     */
    public function save(){
        $this->check();
        $model = new OrderSignRecordModel();
        $data[$model->_orderId]    = $this->orderId;
        $data[$model->_status]     = 0 ;
        $data[$model->_createId]   = $this->opUid;
        $data[$model->_createTime] = time();
        $result =  $model->insert($data);
        if(!$result){
            exception('添加签收记录失败');
        }
        return true;
    }
}